# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class TreeNode:
    def __init__(self, val=None, left=None,right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
        def tree_order(node: TreeNode, result: List[int]):
            if node is None:
                return

            tree_order(node.left, result)
            result.append(node.val)
            tree_order(node.right, result)

        def merge_list(list1: List[int], list2: List[int]) -> List[int]:
            res = []
            while list1 and list2:
                min_val = list1.pop(0) if list1[0] < list2[0] else list2.pop(0)
                res.append(min_val)

            res += list1 if list1 else list2
            return res

        root1_list = []
        tree_order(root1, root1_list)
        root2_list = []
        tree_order(root2, root2_list)

        return merge_list(root1_list, root2_list)


if __name__ == '__main__':


    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ([2, 1, 4], [1, 0, 3], [0, 1, 1, 2, 3, 4]),
        ([1, None, 8], [8, None, 1], [1, 1, 8, 8])
    ]

    for case_item in testcases:
        solution = Solution()
        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]

        exec_result = solution.getAllElements(input1, input2)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

